package fr.univ.surveyonline.model;

import java.util.Calendar;
import java.util.Date;

public class DateHelpers {

	public static Date getDate(String s) {
		if ((s.length() != 10) || (s.charAt(2) != '/') ||(s.charAt(5) != '/')) {
			throw new IllegalStateException("Format de date invalide : " + s);
		}
		Calendar c = Calendar.getInstance(); 
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		
		Integer jour = Integer.valueOf(s.substring(0, 2));
        Integer mois = Integer.valueOf(s.substring(3, 5));
        Integer annee = Integer.valueOf(s.substring(6, 10));
        
        c.set(Calendar.DAY_OF_MONTH, jour);
        c.set(Calendar.YEAR, annee);
        switch (mois) {
        	case 1 : c.set(Calendar.MONTH, Calendar.JANUARY);break;
        	case 2 : c.set(Calendar.MONTH, Calendar.FEBRUARY);break;
        	case 3 : c.set(Calendar.MONTH, Calendar.MARCH);break;
        	case 4 : c.set(Calendar.MONTH, Calendar.APRIL);break;
        	case 5 : c.set(Calendar.MONTH, Calendar.MAY);break;
        	case 6 : c.set(Calendar.MONTH, Calendar.JUNE);break;
        	case 7 : c.set(Calendar.MONTH, Calendar.JULY);break;
        	case 8 : c.set(Calendar.MONTH, Calendar.AUGUST);break;
        	case 9 : c.set(Calendar.MONTH, Calendar.SEPTEMBER);break;
        	case 10 : c.set(Calendar.MONTH, Calendar.OCTOBER);break;
        	case 11 :c.set(Calendar.MONTH, Calendar.NOVEMBER);break;
        	case 12 : c.set(Calendar.MONTH, Calendar.DECEMBER);break;
        	default : throw new IllegalStateException("Format de date invalide : " + s); 
        }

        if (!dateToString(c.getTime()).equals(s)) {
        	throw new IllegalStateException("Format de date invalide : " + s);
        }
        return c.getTime();
	}
	
	public static String dateToString(Date d) {
		if (d == null) {
			return "";
		} else {
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			String mois = null;
		    switch (c.get(Calendar.MONTH)) {
	        	case Calendar.JANUARY : mois = "01";break;
	        	case Calendar.FEBRUARY : mois = "02";break;
	        	case Calendar.MARCH : mois = "03";break;
	        	case Calendar.APRIL : mois = "04";break;
	        	case Calendar.MAY : mois = "05";break;
	        	case Calendar.JUNE : mois = "06";break;
	        	case Calendar.JULY : mois = "07";break;
	        	case Calendar.AUGUST : mois = "08";break;
	        	case Calendar.SEPTEMBER : mois = "09";break;
	        	case Calendar.OCTOBER : mois = "10";break;
	        	case Calendar.NOVEMBER : mois = "11";break;
	        	case Calendar.DECEMBER : mois = "12";break;
	        	default : throw new IllegalStateException("Format de date invalide"); 
	        }
		    String jour = c.get(Calendar.DAY_OF_MONTH) + "";
		    if (jour.length() == 1) {
		    	jour = "0" + jour;
		    }
		    
			return jour + "/" + mois + "/" + c.get(Calendar.YEAR);
		}
	}
	
	public static Date getDate(int year, int month, int day) {
		return getDate(day + "/" + month + "/" + year);
	}
	
	public static Date getTodayDate() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}
}
